
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  
<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/deployment/fastcgi/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>FastCGI &#8212; Werkzeug Documentation (1.0.x)</title>
    <link rel="stylesheet" href="../../_static/werkzeug.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../../../../../assets.readthedocs.org/static/css/badge_only.css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script async="async" type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-doc-embed.js"></script>
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
    <link rel="index" title="Index" href="../../genindex/index.html" />
    <link rel="search" title="Search" href="../../search/index.html" />
    <link rel="next" title="HTTP Proxying" href="../proxying/index.html" />
    <link rel="prev" title="mod_wsgi (Apache)" href="../mod_wsgi/index.html" />
    <link rel="canonical" href="index.html">
  <script>DOCUMENTATION_OPTIONS.URL_ROOT = '../../index.html';</script>
   
  
<!-- RTD Extra Head -->

<!-- 
Always link to the latest version, as canonical.
http://docs.readthedocs.org/en/latest/canonical.html
-->
<link rel="canonical" href="index.html" />

<link rel="stylesheet" href="../../../../../assets.readthedocs.org/static/css/readthedocs-doc-embed.css" type="text/css" />

<script type="text/javascript" src="../../_static/readthedocs-data.js"></script>

<!-- Add page-specific data, which must exist in the page js, not global -->
<script type="text/javascript">
READTHEDOCS_DATA['page'] = "deployment/fastcgi"
READTHEDOCS_DATA['source_suffix'] = ".rst"
</script>

<script type="text/javascript" src="../../../../../assets.readthedocs.org/static/javascript/readthedocs-analytics.js" async="async"></script>

<!-- end RTD <extrahead> -->
</head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex/index.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex/index.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="../proxying/index.html" title="HTTP Proxying"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="../mod_wsgi/index.html" title="mod_wsgi (Apache)"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">Werkzeug Documentation (1.0.x)</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Application Deployment</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="fastcgi">
<h1>FastCGI<a class="headerlink" href="#fastcgi" title="Permalink to this headline">¶</a></h1>
<p>A very popular deployment setup on servers like <a class="reference external" href="https://www.lighttpd.net/">lighttpd</a> and <a class="reference external" href="https://nginx.org/">nginx</a>
is FastCGI.  To use your WSGI application with any of them you will need
a FastCGI server first.</p>
<p>The most popular one is <a class="reference external" href="https://pypi.org/project/flup/">flup</a> which we will use for this guide.  Make
sure to have it installed.</p>
<div class="section" id="creating-a-fcgi-file">
<h2>Creating a <cite>.fcgi</cite> file<a class="headerlink" href="#creating-a-fcgi-file" title="Permalink to this headline">¶</a></h2>
<p>First you need to create the FastCGI server file.  Let’s call it
<cite>yourapplication.fcgi</cite>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="ch">#!/usr/bin/python</span>
<span class="kn">from</span> <span class="nn">flup.server.fcgi</span> <span class="k">import</span> <span class="n">WSGIServer</span>
<span class="kn">from</span> <span class="nn">yourapplication</span> <span class="k">import</span> <span class="n">make_app</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">application</span> <span class="o">=</span> <span class="n">make_app</span><span class="p">()</span>
    <span class="n">WSGIServer</span><span class="p">(</span><span class="n">application</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</pre></div>
</div>
<p>This is enough for Apache to work, however ngingx and older versions of
lighttpd need a socket to be explicitly passed to communicate with the FastCGI
server.  For that to work you need to pass the path to the socket to the
<code class="xref py py-class docutils literal notranslate"><span class="pre">WSGIServer</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">WSGIServer</span><span class="p">(</span><span class="n">application</span><span class="p">,</span> <span class="n">bindAddress</span><span class="o">=</span><span class="s1">&#39;/path/to/fcgi.sock&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">()</span>
</pre></div>
</div>
<p>The path has to be the exact same path you define in the server
config.</p>
<p>Save the <cite>yourapplication.fcgi</cite> file somewhere you will find it again.
It makes sense to have that in <cite>/var/www/yourapplication</cite> or something
similar.</p>
<p>Make sure to set the executable bit on that file so that the servers
can execute it:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># chmod +x /var/www/yourapplication/yourapplication.fcgi</span>
</pre></div>
</div>
</div>
<div class="section" id="configuring-lighttpd">
<h2>Configuring lighttpd<a class="headerlink" href="#configuring-lighttpd" title="Permalink to this headline">¶</a></h2>
<p>A basic FastCGI configuration for lighttpd looks like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">fastcgi</span><span class="o">.</span><span class="n">server</span> <span class="o">=</span> <span class="p">(</span><span class="s2">&quot;/yourapplication.fcgi&quot;</span> <span class="o">=&gt;</span>
    <span class="p">((</span>
        <span class="s2">&quot;socket&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;/tmp/yourapplication-fcgi.sock&quot;</span><span class="p">,</span>
        <span class="s2">&quot;bin-path&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;/var/www/yourapplication/yourapplication.fcgi&quot;</span><span class="p">,</span>
        <span class="s2">&quot;check-local&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;disable&quot;</span><span class="p">,</span>
        <span class="s2">&quot;max-procs&quot;</span> <span class="o">-&gt;</span> <span class="mi">1</span>
    <span class="p">))</span>
<span class="p">)</span>

<span class="n">alias</span><span class="o">.</span><span class="n">url</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s2">&quot;/static/&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;/path/to/your/static&quot;</span>
<span class="p">)</span>

<span class="n">url</span><span class="o">.</span><span class="n">rewrite</span><span class="o">-</span><span class="n">once</span> <span class="o">=</span> <span class="p">(</span>
    <span class="s2">&quot;^(/static.*)$&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;$1&quot;</span><span class="p">,</span>
    <span class="s2">&quot;^(/.*)$&quot;</span> <span class="o">=&gt;</span> <span class="s2">&quot;/yourapplication.fcgi$1&quot;</span>
</pre></div>
</div>
<p>Remember to enable the FastCGI, alias and rewrite modules. This configuration
binds the application to <cite>/yourapplication</cite>.</p>
<p>See the Lighty docs for more information on <a class="reference external" href="https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_ModFastCGI">FastCGI and Python</a>.</p>
</div>
<div class="section" id="configuring-nginx">
<h2>Configuring nginx<a class="headerlink" href="#configuring-nginx" title="Permalink to this headline">¶</a></h2>
<p>Installing FastCGI applications on nginx is a bit tricky because by default
some FastCGI parameters are not properly forwarded.</p>
<p>A basic FastCGI configuration for nginx looks like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>location /yourapplication/ {
    include fastcgi_params;
    if ($uri ~ ^/yourapplication/(.*)?) {
        set $path_url $1;
    }
    fastcgi_param PATH_INFO $path_url;
    fastcgi_param SCRIPT_NAME /yourapplication;
    fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;
}
</pre></div>
</div>
<p>This configuration binds the application to <cite>/yourapplication</cite>.  If you want
to have it in the URL root it’s a bit easier because you don’t have to figure
out how to calculate <cite>PATH_INFO</cite> and <cite>SCRIPT_NAME</cite>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>location /yourapplication/ {
    include fastcgi_params;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_param SCRIPT_NAME &quot;&quot;;
    fastcgi_pass unix:/tmp/yourapplication-fcgi.sock;
}
</pre></div>
</div>
<p>Since Nginx doesn’t load FastCGI apps, you have to do it by yourself.  You
can either write an <cite>init.d</cite> script for that or execute it inside a screen
session:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ screen
$ /var/www/yourapplication/yourapplication.fcgi
</pre></div>
</div>
</div>
<div class="section" id="debugging">
<h2>Debugging<a class="headerlink" href="#debugging" title="Permalink to this headline">¶</a></h2>
<p>FastCGI deployments tend to be hard to debug on most webservers.  Very often the
only thing the server log tells you is something along the lines of “premature
end of headers”.  In order to debug the application the only thing that can
really give you ideas why it breaks is switching to the correct user and
executing the application by hand.</p>
<p>This example assumes your application is called <cite>application.fcgi</cite> and that your
webserver user is <cite>www-data</cite>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>$ su www-data
$ cd /var/www/yourapplication
$ python application.fcgi
Traceback (most recent call last):
  File &quot;yourapplication.fcg&quot;, line 4, in &lt;module&gt;
ImportError: No module named yourapplication
</pre></div>
</div>
<p>In this case the error seems to be “yourapplication” not being on the python
path.  Common problems are:</p>
<ul class="simple">
<li>relative paths being used.  Don’t rely on the current working directory</li>
<li>the code depending on environment variables that are not set by the
web server.</li>
<li>different python interpreters being used.</li>
</ul>
</div>
</div>


          </div>
        </div>
      </div>
  <span id="sidebar-top"></span>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  
    
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/werkzeug.png" alt="Logo"/>
            </a></p>
  

  <h3>Contents</h3>
  <ul>
<li><a class="reference internal" href="#">FastCGI</a><ul>
<li><a class="reference internal" href="#creating-a-fcgi-file">Creating a <cite>.fcgi</cite> file</a></li>
<li><a class="reference internal" href="#configuring-lighttpd">Configuring lighttpd</a></li>
<li><a class="reference internal" href="#configuring-nginx">Configuring nginx</a></li>
<li><a class="reference internal" href="#debugging">Debugging</a></li>
</ul>
</li>
</ul>
<h3>Navigation</h3>
<ul>
  <li><a href="../../index.html">Overview</a>
    <ul>
      <li><a href="../index.html">Application Deployment</a>
        <ul>
          <li>Previous: <a href="../mod_wsgi/index.html" title="previous chapter"><cite>mod_wsgi</cite> (Apache)</a>
          <li>Next: <a href="../proxying/index.html" title="next chapter">HTTP Proxying</a></ul>
      </li>
    </ul>
  </li>
</ul>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://werkzeug.palletsprojects.com/en/1.0.x/search/" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
  
    <div class="footer" role="contentinfo">
        &#169; Copyright 2007 Pallets.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.8.5.
    </div>
    <script>
      if (typeof READTHEDOCS_DATA !== 'undefined') {
        if (!READTHEDOCS_DATA.features) {
          READTHEDOCS_DATA.features = {};
        }
        READTHEDOCS_DATA.features.docsearch_disabled = true;
      }
    </script>

  </body>

<!-- Mirrored from werkzeug.palletsprojects.com/en/1.0.x/deployment/fastcgi/ by HTTrack Website Copier/3.x [XR&CO'2014], Tue, 15 Sep 2020 06:37:09 GMT -->
</html>